package com.googlecode.recycled.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.oxm.jibx.JibxMarshaller;

import java.util.List;

/**
 * @author Henryk Konsek
 */
public class JibxMarshallersFactory implements BeanDefinitionRegistryPostProcessor {

    protected List<Class> targetClasses;

    protected int indent = -1;

    public void setTargetClasses(List<Class> targetClasses) {
        this.targetClasses = targetClasses;
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        for (Class targetClass : targetClasses) {
            BeanDefinition beanDefinition = new RootBeanDefinition(JibxMarshaller.class);
            beanDefinition.getPropertyValues().add("targetClass", targetClass);
            beanDefinition.getPropertyValues().add("indent", indent);
            registry.registerBeanDefinition("jibxMarshaller:" + targetClass.getName(), beanDefinition);
        }
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    }

    public void setIndent(int indent) {
        this.indent = indent;
    }

}
